package com.voc.im.controller;

import com.voc.im.bean.*;
import com.voc.im.controller.item.ItemSendController;
import com.voc.im.bean.UserRoom;
import com.voc.im.model.*;
import io.datafx.controller.FXMLController;
import io.datafx.controller.flow.Flow;
import io.datafx.controller.flow.FlowException;
import javafx.event.Event;
import javafx.event.EventHandler;
import javafx.fxml.FXML;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.ListCell;
import javafx.scene.control.ListView;
import javafx.scene.image.ImageView;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
import javafx.stage.StageStyle;

import javax.annotation.PostConstruct;
import javax.inject.Inject;
import java.lang.ref.WeakReference;
import java.util.HashMap;
import java.util.Map;

@FXMLController("/fxml/main_window_chat.fxml")
public class WindowChatController {

    @FXML
    private AnchorPane test_anchor_pane;

    @FXML
    private ListView<ChatRecord> list_main_window;

    @FXML
    private Label label_name;

    @FXML
    private ImageView image_user_more;

    @Inject
    private ChatRecordModel chatRecordModel;

    @FXML
    private Button btnMainChat;

    @PostConstruct
    public void init() throws Exception {
        UserRoom userRoom = chatRecordModel.getObjectProperty().getValue();
        label_name.textProperty().bindBidirectional(userRoom.nicknameProperty());
        list_main_window.setCellFactory((ListView<ChatRecord> listView) -> new ChatRecordCell(this));
        list_main_window.itemsProperty().bind(chatRecordModel.getListPropertyMap().get(userRoom.getId()));
        chatRecordModel.getSelectedRecordIndex().bind(list_main_window.getSelectionModel().selectedIndexProperty());

        image_user_more.setOnMouseClicked(event -> {
            try {
                StackPane root = new Flow(DialogController.class).start();
                Scene scene = new Scene(root);
                Stage stage = new Stage();
                stage.setX(image_user_more.getScene().getWindow().getX() + image_user_more.getScene().getWindow().getWidth());
                stage.setY(image_user_more.getScene().getWindow().getY());
                stage.initStyle(StageStyle.UNDECORATED);
                stage.initOwner(image_user_more.getScene().getWindow());
                stage.setScene(scene);
                stage.show();
            } catch (FlowException ex) {
                ex.printStackTrace();
            }
        });
        image_user_more.setOnMouseDragged(Event::consume);

        label_name.setOnMouseClicked(new EventHandler<MouseEvent>() {
            @Override
            public void handle(MouseEvent event) {
//                StackPane stackPane = (StackPane) label_name.getScene().getRoot();
                System.out.println(test_anchor_pane.getScene().getWindow());
//                stackPane.getChildren().clear();
            }
        });
    }


    private Map<Integer, StackPane> stackPaneMap = new HashMap<>();


    /**
     * 消息记录列表item更新
     */
    static class ChatRecordCell extends ListCell<ChatRecord> {

        private WeakReference<WindowChatController> weakReference;

        private ChatRecordCell(WindowChatController controller) {
            this.weakReference = new WeakReference<>(controller);
        }

        @Override
        protected void updateItem(ChatRecord item, boolean empty) {
            super.updateItem(item, empty);
            WindowChatController controller = weakReference.get();
            if (controller == null) {
                return;
            }
            if (item != null) {
                controller.chatRecordModel.getSelectedRecordIndex().bind(indexProperty());
                StackPane stackPane = controller.stackPaneMap.get(indexProperty().intValue());
                try {
                    if (stackPane == null)
                        stackPane = new Flow(ItemSendController.class).start();
                    controller.stackPaneMap.put(indexProperty().intValue(), stackPane);
                } catch (FlowException e) {
                    e.printStackTrace();
                }
                setGraphic(stackPane);
            } else {
                setGraphic(null);
            }
        }


    }
}
